import { Inject, Injectable } from '@nestjs/common';
import OSS from 'ali-oss';

@Injectable()
export class OssService {
  constructor(@Inject('OSS_CLIENT') private readonly client: OSS) {}

  async uploadFile(file: Express.Multer.File, path: string): Promise<string> {
    console.log('file.buffer', file.buffer);
    const result = await this.client.put(path, file.buffer);
    return result.url;
  }

  async getFileUrl(fileName: string): Promise<string> {
    return this.client.signatureUrl(fileName, { expires: 3600 });
  }

  async deleteFile(fileName: string): Promise<void> {
    await this.client.delete(fileName);
  }
}
